
package com.bbh.plugin.single.privilege.demo.controller.mail;

import com.bbh.plugin.single.privilege.core.entity.App;
import com.bbh.plugin.single.privilege.core.entity.MailAccount;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.service.AppService;
import com.bbh.plugin.single.privilege.core.service.MailAccountService;
import com.bbh.plugin.single.privilege.demo.controller.BasicController;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.Page;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.config.mvc.interceptors.log.Log;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Controller - 邮件管理
 *
 * @author ZHANGTYONG
 * @version 3.0
 */
@Controller
@RequestMapping("/mail/account")
public class MailAccountController extends BasicController {
    @Autowired
    private MailAccountService mailAccountService;
    @Autowired
    private AppService appService;
    @Value("${system.appKey}")
    private String systemAppKey;

    /**
     * 跳转到邮件账号管理页面
     */
    @RequestMapping(value = VIEW_FILED + "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        Condition condition = new Condition();
        condition.putEquals("appKey", systemAppKey);
        condition.putEquals("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        condition.putEquals("isEnabled", BaseEnum.IS_ENABLED.ENABLED.getKey());
        List<App> appList = appService.selectList(condition, null);
        model.addAttribute("appList", appList);
        return "single/mail/mailAccount";
    }

    /**
     * 邮件账号列表
     *
     * @return
     * @throws
     */
    @RequestMapping(value = VIEW_FILED + "/mailAccountList")
    @ResponseBody
    public GridVo mailAccountList(HttpServletRequest request, String appKey, String mailAccountType) throws Exception {
        if (StringUtils.isBlank(appKey)) {
            appKey = systemAppKey;
        }
        Condition params = new Condition();
        params.putEquals("appKey", appKey);
        params.putNotBlank("mailAccountType", Condition.LIKE, mailAccountType);
        Page<MailAccount> pageResult = mailAccountService.selectPage(params, null, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));
        return new GridVo(pageResult);
    }

    /**
     * 跳转到mailAccount edit页面
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/edit")
    public String edit(String id, Model model, String type) {

        if ("edit".equals(type)) {
            Assert.notNull(id, "参数错误");
            MailAccount mailAccount = mailAccountService.selectByPrimaryKey(id);
            model.addAttribute("mailAccount", mailAccount);
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            model.addAttribute(OPERATE_TYPE, "add");
        }
        Condition condition = new Condition();
        condition.putEquals("appKey", systemAppKey);
        condition.putEquals("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        condition.putEquals("isEnabled", BaseEnum.IS_ENABLED.ENABLED.getKey());
        List<App> appList = appService.selectList(condition,null);
        model.addAttribute("appList", appList);
        return "single/mail/mailAccountEdit";
    }

    @Log(tag = "BOSS_MAIL_ACCOUNT", note = "邮件账号保存")
    @RequestMapping(value = INSERT_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Validated MailAccount mailAccount) {
        Date date = new Date();
        mailAccount.setCreateTime(date);
        mailAccount.setModifyTime(date);
        mailAccount.setCreateBy(getLoginName());
        mailAccount.setModifyBy(getLoginName());
        mailAccount.setId(IdUtils64.getInstance().getStringId());
        mailAccount.setDefaultEncoding("UTF-8");
        mailAccountService.insert(mailAccount);
        return new Message(StatusType.SUCCESS.getKey(), "增加成功");
    }


    @RequestMapping(value = UPDATE_FILED + "/update")
    public
    @ResponseBody
    Message update(@Validated MailAccount mailAccount) {
        Assert.notNull(mailAccount.getId(), "参数错误");
        MailAccount modifyMailAccount = mailAccountService.selectByPrimaryKey(mailAccount.getId());
        if (modifyMailAccount != null) {
            modifyMailAccount.setAppKey(mailAccount.getAppKey());
            modifyMailAccount.setModifyTime(new Date());
            modifyMailAccount.setDescription(mailAccount.getDescription());
            modifyMailAccount.setUsername(mailAccount.getUsername());
            modifyMailAccount.setPassword(mailAccount.getPassword());
            modifyMailAccount.setModifyBy(getLoginName());
            modifyMailAccount.setHost(mailAccount.getHost());
            modifyMailAccount.setMailAccountType(mailAccount.getMailAccountType());
            modifyMailAccount.setPort(mailAccount.getPort());
            modifyMailAccount.setSmtpAuth(mailAccount.getSmtpAuth());
            modifyMailAccount.setSmtpSsl(mailAccount.getSmtpSsl());
            modifyMailAccount.setSmtpStarttlsEnable(mailAccount.getSmtpStarttlsEnable());
            mailAccountService.updateByPrimaryKey(mailAccount);
            return new Message(StatusType.SUCCESS.getKey(), "修改成功");
        }
        return new Message(StatusType.ERROR.getKey(), "修改失败");
    }


    @RequestMapping(DELETE_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        if (ArrayUtils.isNotEmpty(ids)) {
            mailAccountService.delete(ids);
            return new Message(StatusType.SUCCESS.getKey(), "删除成功");
        }
        return new Message(StatusType.ERROR.getKey(), "删除失败");
    }

}